package com.airnewslinkosky.ticket.ui.flight;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.CabinItem;
import com.airnewslinkosky.ticket.model.FlightInfo;
import com.airnewslinkosky.ticket.model.FlightRestrict;
import com.airnewslinkosky.ticket.model.SearchFlight;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.ui.login.Login;
import com.airnewslinkosky.ticket.util.AddrUtil;
import com.airnewslinkosky.ticket.util.Constants;
import com.airnewslinkosky.ticket.util.DateUtil;
import com.airnewslinkosky.ticket.util.FlyUtil;
import com.airnewslinkosky.ticket.util.StringUtil;
import com.mobclick.android.MobclickAgent;

public class FlyRoundDetail extends BaseActivity implements
		AdapterView.OnItemClickListener {

//	public static List<Flight1> flist;
	public static FlightInfo firstFlight;
	public static FlightInfo flight;
	public static SearchFlight searchFlight;
	private String ftype;//单程还是双程
	public static List<FlightInfo> flist;
	
	public static FlightRestrict flightRestrict;
	public static FlightRestrict returnFlightRestrict;
	
	private SimpleAdapter sa;
	private ListView lv;
	private RelativeLayout mBtnNext;
	private ImageView mBtnBack;
	List<Map<String, String>> data = new ArrayList<Map<String, String>>();
	private ImageView backBut;
	private ProgressDialog progressDialog = null;
	
	private FlyUtil flyUtil;
	private SharedPreferences sp;
	
	private float sumDoublePrice = 0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fly_round_detail);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
        
		activityList.add(this);
		flyUtil = new FlyUtil(this);
		AddrUtil addrUtil = new AddrUtil(this);
		
		TextView departureDate = (TextView) findViewById(R.id.departureDate);
		String departureTime=firstFlight.departureTime;
		if(departureTime!=null&&departureTime.length()>=10){
			departureTime = departureTime.substring(0,10);
		}
		departureDate.setText("去程 " + departureTime);
		TextView departureWeek = (TextView) findViewById(R.id.departureWeek);
		departureWeek.setText(DateUtil.getWeek("yyyy-MM-dd", departureTime));
		
		TextView fromCity = (TextView) findViewById(R.id.fromCity);
		fromCity.setText(addrUtil.getCityByCode(firstFlight.departure).getCity());
		TextView toCity = (TextView) findViewById(R.id.toCity);
		toCity.setText(addrUtil.getCityByCode(firstFlight.arrival).getCity());
		
		TextView fromtime = (TextView) findViewById(R.id.fromtime);
		fromtime.setText(StringUtil.getTime(firstFlight.departureTime));
		TextView totime = (TextView) findViewById(R.id.totime);
		totime.setText(StringUtil.getTime(firstFlight.arrivalTime));
		TextView fromaf = (TextView) findViewById(R.id.fromaf);
		fromaf.setText(addrUtil.getCityByCode(firstFlight.departure)
				.getAirName());
		TextView toaf = (TextView) findViewById(R.id.toaf);
		toaf.setText(addrUtil.getCityByCode(firstFlight.arrival).getAirName());
		TextView flightno = (TextView) findViewById(R.id.flightNo);
		flightno.setText( flyUtil.getName(firstFlight.airline)+ " " + firstFlight.flightNo);
		TextView airc = (TextView) findViewById(R.id.airc);
		airc.setText(firstFlight.planeType);
		
		TextView stopNo = (TextView) findViewById(R.id.stopNo);
		stopNo.setText(String.valueOf(firstFlight.stopNo));
		
		String food = firstFlight.isFood == 0 ? "有" : "无";
		TextView foodText = (TextView) findViewById(R.id.food);
		foodText.setText(food);
		
		TextView cabinPrice = (TextView) findViewById(R.id.cabinPrice);
		TextView price = (TextView) findViewById(R.id.price);
		TextView fuelTax = (TextView) findViewById(R.id.fuelTax);
		TextView baseCabin = (TextView) findViewById(R.id.baseCabin);
		TextView basePrice = (TextView) findViewById(R.id.basePrice);
		TextView discount = (TextView) findViewById(R.id.discount);
		
		TextView airc0 = (TextView) findViewById(R.id.airc0);//更改条件
		TextView others = (TextView) findViewById(R.id.others);//退票条件
		TextView airc1 = (TextView) findViewById(R.id.airc1);//签转条件
		TextView airc2 = (TextView) findViewById(R.id.airc2);//备　　注
		
		List<CabinItem> cabinlist = firstFlight.cabinItems;
		if(cabinlist!=null&&cabinlist.size()>0){
        	CabinItem cabinItem= cabinlist.get(0);//cabinlist.size()-1
			baseCabin.setText(FlyUtil.getJClassName(cabinItem.baseCabin) + " " + cabinItem.baseCabin);
        	String priceFormat = cabinItem.price.indexOf(".00") != -1 ? cabinItem.price.substring(0, cabinItem.price.indexOf(".00")) : cabinItem.price;
        	String cabinPriceFormat = cabinItem.cabinPrice.indexOf(".00") != -1 ? cabinItem.cabinPrice.substring(0, cabinItem.cabinPrice.indexOf(".00")) : cabinItem.cabinPrice;
			cabinPrice.setText("￥" +cabinPriceFormat + "元");
			price.setText("￥" + priceFormat.trim()  + "元");
			discount.setText("(" + FlyUtil.parseDiscount(cabinItem.discount)  + ")");
			discount.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
			String basePriceV = String.valueOf(Float.valueOf(cabinPriceFormat) - Float.valueOf(priceFormat));
			String basePriceFormat = basePriceV.indexOf(".0") != -1 ? basePriceV.substring(0, basePriceV.indexOf(".0")) : basePriceV;
			//basePrice.setText(" 返还" + basePriceFormat + "元");
			basePrice.setText(" 返还100元");
			cabinPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
			String airportTaxV = (cabinItem.airportTax.equals("") ||  (cabinItem.airportTax == null) ? "0" : cabinItem.airportTax) ;
			String fuelTaxV = (cabinItem.fuelTax.equals("") || (cabinItem.fuelTax == null) ? "0" : cabinItem.fuelTax);
			fuelTax.setText("￥" + airportTaxV + "/￥" + fuelTaxV );
			
			float sumPrice = Float.valueOf(cabinItem.price) + Float.valueOf(fuelTaxV) + Float.valueOf(fuelTaxV);
			
			sumDoublePrice = sumDoublePrice + sumPrice;
			
			others.setText(flightRestrict.airc0);
		}
		
		
		
		TextView t_departureDate = (TextView) findViewById(R.id.t_departureDate);
		String t_departureTime=flight.departureTime;
		if(t_departureTime!=null&&t_departureTime.length()>=10){
			t_departureTime = t_departureTime.substring(0,10);
		}
		t_departureDate.setText("回程 " + t_departureTime);
		TextView t_departureWeek = (TextView) findViewById(R.id.t_departureWeek);
		t_departureWeek.setText(DateUtil.getWeek("yyyy-MM-dd", t_departureTime));
		
		TextView t_fromCity = (TextView) findViewById(R.id.t_fromCity);
		t_fromCity.setText(addrUtil.getCityByCode(flight.departure).getCity());
		TextView t_toCity = (TextView) findViewById(R.id.t_toCity);
		t_toCity.setText(addrUtil.getCityByCode(flight.arrival).getCity());
		
		TextView t_fromtime = (TextView) findViewById(R.id.t_fromtime);
		t_fromtime.setText(StringUtil.getTime(flight.departureTime));
		TextView t_totime = (TextView) findViewById(R.id.t_totime);
		t_totime.setText(StringUtil.getTime(flight.arrivalTime));
		TextView t_fromaf = (TextView) findViewById(R.id.t_fromaf);
		t_fromaf.setText(addrUtil.getCityByCode(flight.departure)
				.getAirName());
		TextView t_toaf = (TextView) findViewById(R.id.t_toaf);
		t_toaf.setText(addrUtil.getCityByCode(flight.arrival).getAirName());
		TextView t_flightno = (TextView) findViewById(R.id.t_flightNo);
		t_flightno.setText( flyUtil.getName(flight.airline)+ " " + flight.flightNo);
		TextView t_airc = (TextView) findViewById(R.id.t_airc);
		t_airc.setText(flight.planeType);
		
		TextView t_stopNo = (TextView) findViewById(R.id.t_stopNo);
		t_stopNo.setText(String.valueOf(flight.stopNo));
		
		String t_food = flight.isFood == 0 ? "有" : "无";
		TextView t_foodText = (TextView) findViewById(R.id.t_food);
		t_foodText.setText(t_food);
		
		TextView t_cabinPrice = (TextView) findViewById(R.id.t_cabinPrice);
		TextView t_price = (TextView) findViewById(R.id.t_price);
		TextView t_fuelTax = (TextView) findViewById(R.id.t_fuelTax);
		TextView t_baseCabin = (TextView) findViewById(R.id.t_baseCabin);
		TextView t_basePrice = (TextView) findViewById(R.id.t_basePrice);
		TextView t_discount = (TextView) findViewById(R.id.t_discount);
		
		TextView t_airc0 = (TextView) findViewById(R.id.t_airc0);//更改条件
		TextView t_others = (TextView) findViewById(R.id.t_others);//退票条件
		TextView t_airc1 = (TextView) findViewById(R.id.t_airc1);//签转条件
		TextView t_airc2 = (TextView) findViewById(R.id.t_airc2);//备　　注
		
		List<CabinItem> t_cabinlist = flight.cabinItems;
		if(t_cabinlist!=null&&t_cabinlist.size()>0){
        	CabinItem cabinItem= cabinlist.get(0);//cabinlist.size()-1
        	t_baseCabin.setText(FlyUtil.getJClassName(cabinItem.baseCabin) + " " + cabinItem.baseCabin);
        	String priceFormat = cabinItem.price.indexOf(".00") != -1 ? cabinItem.price.substring(0, cabinItem.price.indexOf(".00")) : cabinItem.price;
        	String cabinPriceFormat = cabinItem.cabinPrice.indexOf(".00") != -1 ? cabinItem.cabinPrice.substring(0, cabinItem.cabinPrice.indexOf(".00")) : cabinItem.cabinPrice;
        	t_cabinPrice.setText("￥" +cabinPriceFormat + "元");
        	t_price.setText("￥" + priceFormat.trim()  + "元");
        	t_discount.setText("(" + FlyUtil.parseDiscount(cabinItem.discount)  + ")");
        	t_discount.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
			String basePriceV = String.valueOf(Float.valueOf(cabinPriceFormat) - Float.valueOf(priceFormat));
			String basePriceFormat = basePriceV.indexOf(".0") != -1 ? basePriceV.substring(0, basePriceV.indexOf(".0")) : basePriceV;
			//basePrice.setText(" 返还" + basePriceFormat + "元");
			t_basePrice.setText(" 返还100元");
			t_cabinPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
			String airportTaxV = (cabinItem.airportTax.equals("") ||  (cabinItem.airportTax == null) ? "0" : cabinItem.airportTax) ;
			String fuelTaxV = (cabinItem.fuelTax.equals("") || (cabinItem.fuelTax == null) ? "0" : cabinItem.fuelTax);
			t_fuelTax.setText("￥" + airportTaxV + "/￥" + fuelTaxV );
			
			float sumPrice = Float.valueOf(cabinItem.price) + Float.valueOf(fuelTaxV) + Float.valueOf(fuelTaxV);
			
			sumDoublePrice =  sumDoublePrice + sumPrice;
			
			t_others.setText(returnFlightRestrict.airc0);
			
		}
		
		TextView sumPriceText = (TextView) findViewById(R.id.sumPrice);
       	String sumPriceFormat = String.valueOf(sumDoublePrice).indexOf(".0") != -1 ? String.valueOf(sumDoublePrice).substring(0, String.valueOf(sumDoublePrice).indexOf(".0")) : String.valueOf(sumDoublePrice);

		sumPriceText.setText(" 双程总支付：" + sumPriceFormat + "元");
		
		mBtnNext = (RelativeLayout)findViewById(R.id.btn_next);
		
		OnClickListener b_ocl= new OnClickListener() { 
	    	public void onClick(View v) { 
	    		switch (v.getId()) {
	    		case R.id.backBut:
	    			Intent intentR = new Intent();
					intentR.setClass(FlyRoundDetail.this, FlyRoundList.class);
					FlyRoundList.searchFlight = searchFlight;
					FlyRoundList.firstFlight = firstFlight;
					FlyRoundList.flight = flight;
					startActivity(intentR);
	    			break;
	    		case R.id.btn_next:
//	    			sp = getSharedPreferences("UserInfoB2B", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
//	    			String userIdLogin = sp.getString("userIdB2B", null);
//	    			String isLoninOK = sp.getString("isLoninOKB2B", null);
//	    	        String autoLogin = sp.getString("isAutoLoginB2B", null);
	    	        
	    	        String userIdLogin = Constants.USERID;
	    	        
	    	        if(userIdLogin!=null && !userIdLogin.equals("")){
	    	        	Intent it = new Intent(FlyRoundDetail.this, Login.class);
	    	        	Login.flight = firstFlight;
	    	        	Login.searchFlight = searchFlight;
	    	        	Login.returnFlight = flight;
	    				startActivity(it);
	    				overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	    	        } else {
	    				Intent intent = new Intent();
	    				intent.setClass(FlyRoundDetail.this, FlyOrderInput.class);
	    				FlyOrderInput.flight = firstFlight;
	    				FlyOrderInput.searchFlight = searchFlight;
	    				FlyOrderInput.returnFlight = flight;
	    				startActivity(intent);
	    	        }
	    			break;
	    		}
	    	}
		};
		
		backBut = (ImageView) findViewById(R.id.backBut);
		backBut.setOnClickListener(b_ocl);
		mBtnNext.setOnClickListener(b_ocl);
	    //new GetWeatherIcon().execute(eDeparture,eArrival);
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent();
		if(flight != null){
			intent.setClass(this, FlightQuery.class);
		} else {
			intent.setClass(this, FlightRoundQuery.class);
		}
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityIfNeeded(intent, -1);
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
